package com.tiger.admin.http.common.menu;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiger.admin.Db;
import com.tiger.admin.SaveOperateLog;
import com.tiger.admin.http.common.member.MemberService;
import com.tiger.admin.http.common.menu.dto.Add;
import com.tiger.admin.http.common.menu.dto.Update;
import com.tiger.db.entity.Menu;
import com.tiger.utils.ResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
public class MenuController {
    private final Db db;
    private final MemberService memberService;

    public MenuController(Db db, MemberService memberService) {
        this.db = db;
        this.memberService = memberService;
    }


    //获取用户可以用的菜单
    @GetMapping("permission")
    public Object permission(@RequestAttribute("memberId") long memberId) {
        List<Menu> menus = memberService.permissionMenu(memberId, "*");
        return ResponseUtil.successRes(menus.stream().filter(Menu::getEnable).collect(Collectors.toList()));
    }

    @GetMapping("list")
    public Object list() {
        QueryWrapper query = new QueryWrapper<Menu>();

        query.orderByDesc("sort");
        query.orderByAsc("id");


        List<Menu> menuList = db.menu.list(query);

        return ResponseUtil.successRes(menuList);
    }

    @SaveOperateLog("新增菜单")
    @PostMapping("add")
    public Object add(@RequestBody Add data) {
        if(db.menu.exists(new QueryWrapper<Menu>().eq("name", data.getName()).eq("parent_id", data.getParentId()))) {
            return ResponseUtil.failRes("已存在相同的名称");
        }

        String type = data.getType();
        if((Objects.equals(type, "FOLD") || Objects.equals(type, "PAGE")) && db.menu.exists(new QueryWrapper<Menu>().eq("path", data.getPath()).eq("parent_id", data.getParentId()))) {
            return ResponseUtil.failRes("已存在相同的路径");
        }

        if(Objects.equals(type, "BUTTON")) {
            data.setComponent(null);
            data.setDisplay(false);
            data.setPath(null);
            data.setKeepAlive(false);
            data.setSort(0);
        }

        Menu menu = new Menu();

        BeanUtils.copyProperties(data, menu);

        boolean r = db.menu.save(menu);

        return r ? ResponseUtil.successRes(menu) : ResponseUtil.failRes();
    }

    @SaveOperateLog("更新菜单")
    @PostMapping("update/{id}")
    public Object add(@RequestBody @Validated Update data, @PathVariable long id) {

        Menu row = db.menu.getById(id);
        if(row == null) return ResponseUtil.failRes();


        if(data.getName() != null && !Objects.equals(data.getName(), row.getName())) {
            if(db.menu.exists(new QueryWrapper<Menu>().eq("name", data.getName()).eq("parent_id", row.getParentId()))) {
                return ResponseUtil.failRes("已存在相同的名称");
            }
        }

        if(data.getPath() != null && !Objects.equals(data.getPath(), row.getPath())) {
            if(db.menu.exists(new QueryWrapper<Menu>().eq("path", data.getPath()).eq("parent_id", row.getParentId()))) {
                return ResponseUtil.failRes("已存在相同的路径");
            }
        }



        BeanUtils.copyProperties(data, row);



        boolean r = db.menu.updateById(row);
        return r ? ResponseUtil.successRes(row) : ResponseUtil.failRes();
    }


    @SaveOperateLog("删除菜单")
    @PostMapping("delete/{id}")
    public Object del(@PathVariable long id) {
        if(db.menu.exists(new QueryWrapper<Menu>().eq("parent_id", id))) {
            return ResponseUtil.failRes("存在子菜单，不可删除");
        }
        boolean r = db.menu.removeById(id);
        return r ? ResponseUtil.successRes() : ResponseUtil.failRes();
    }
}
